package com.china.milkywayeye.algorithm;

import java.util.*;

/**
 * @Program: java-ceshiuse
 * @ClassName: Demo
 * @Description: 力扣题
 * @Author: milkywayeye
 * @Create: 2024-04-29 15:00
 * @Version: v1.0
 */
public class Demo {
    public static void main(String[] args) {
//        System.out.println(findMedianSortedArrays(new int[]{1,3}, new int[]{2}));
        int[] a = {1, 2};
        int[] b = {3, 4};
        System.out.println(findMedianSortedArrays(a, b));
    }

    public static int lengthOfLongestSubstring(String s) {
        if (s.equals("")) {
            return 0;
        }
        int result = 0;
        int ing = 0;
        int m = 0, n = 0;
        int l = s.length();
        Map<Character, Integer> map = new HashMap<>();
        while (l - n + ing > result) {
            if (map.containsKey(s.charAt(n))) {
                int e = map.get(s.charAt(n));
                List<Character> characters = new LinkedList<>();
                for (Character ch : map.keySet()) {
                    if (map.get(ch) <= e) {
                        characters.add(ch);
                    }
                }
                for (Character ch : characters) {
                    ing--;
                    m++;
                    map.remove(ch);
                }
            }
            map.put(s.charAt(n), n);
            ing++;
            n++;
            result = result < ing ? ing : result;
        }
        return result;
    }

    public static double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int[] nums = new int[nums1.length + nums2.length];
        int m = 0, n = 0;
        int ing = 0;
        while (m < nums1.length && n < nums2.length) {
            if (nums1[m] < nums2[n]) {
                nums[ing] = nums1[m];
                m++;
            } else {
                nums[ing] = nums2[n];
                n++;
            }
            ing++;
        }
        if (m < nums1.length) {
            System.arraycopy(nums1, m, nums, ing, nums1.length - m);
        } else if (n < nums2.length) {
            System.arraycopy(nums2, n, nums, ing, nums2.length - n);
        }
        double result = 0;
        int l = nums.length;
        System.out.println(Arrays.toString(nums));
        if (l % 2 == 1) {
            result = nums[l / 2];
        } else {
            result = (double) (nums[l / 2] + nums[l / 2 - 1]) / 2;
        }
        return result;
    }

    public boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        if (x <= 9) {
            return true;
        }
        String s = String.valueOf(x);
        int l = s.length();
        for (int i = 0; i < l / 2; i++) {
            if (s.charAt(i) != s.charAt(l - 1 - i)) {
                return false;
            }
        }
        return true;
    }
}



